package edu.langnan.xiaoli.service.impl;

import edu.langnan.xiaoli.constant.XiaoLiConstants;
import edu.langnan.xiaoli.core.domain.LostDO;
import edu.langnan.xiaoli.core.dto.LostDTO;
import edu.langnan.xiaoli.mapper.LostMapper;
import edu.langnan.xiaoli.service.LostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author cql
 */
@Service
public class LostServiceImpl implements LostService {

    @Autowired
    private LostMapper lostMapper;

    @Override
    public void saveOrUpdate(LostDO lost) {
        // 修改
        if (lost.getId() != null && lost.getId() != 0) {
            lostMapper.updateLost(lost);
        } else {
            // 发布时间
            lost.setCreateTime(new Date());
            // 当前状态:已完成
            lost.setStatus(XiaoLiConstants.NO_STATUS);
            // 新增
            lostMapper.insert(lost);
        }
    }

    @Override
    public List<LostDTO> findLostList(String lostType, String status, Integer stuId) {
        List<LostDTO> result = lostMapper.findLostList(lostType, status, stuId);
        for (LostDTO lostDTO : result) {
            String imgs = lostDTO.getImgs();
            String[] imgUrls = imgs.split(XiaoLiConstants.SEPARATOR);
            lostDTO.setImgUrls(imgUrls);
        }
        return result;
    }

    @Override
    public LostDTO findLostById(Integer lostId) {
        LostDTO result = lostMapper.findLostById(lostId);
        String imgs = result.getImgs();
        String[] imgUrls = imgs.split(XiaoLiConstants.SEPARATOR);
        result.setImgUrls(imgUrls);
        return result;
    }
}
